package com.example.myapp;

import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.ContentResolver;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapp.sms.SmsInfo;

import java.util.ArrayList;
import java.util.List;

public class SmsActivity extends AppCompatActivity {
    private TextView tvSms;
    private Button btnSms;
    private String text = "";
    private List<SmsInfo> smsInfoList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sms);
        tvSms = (TextView) findViewById(R.id.tvSms);
        btnSms = (Button) findViewById(R.id.btnSms);
        smsInfoList = new ArrayList<SmsInfo>();
        btnSms.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ActivityCompat.requestPermissions(
                        SmsActivity.this,
                        new String[]{Manifest.permission.READ_SMS}, 1);
            }
        });
    }
    public void getSms() {
        Uri uri = Uri.parse("content://sms/");
        ContentResolver resolver = getContentResolver();
        Cursor cursor = resolver.query(
                uri, new String[]{"_id", "address", "body"},
                null, null, null);
        if (cursor != null && cursor.getCount() > 0) {
            if (smsInfoList != null) smsInfoList.clear();
            text = "";
            while (cursor.moveToNext()) {
                int _id = cursor.getInt(0);
                String address = cursor.getString(1);
                String body = cursor.getString(2);
                SmsInfo smsInfo = new SmsInfo(_id, address, body);
                smsInfoList.add(smsInfo);
            }
            cursor.close();
        }
        for (int i = 0; i < smsInfoList.size(); i++) {
            text += "手机号码：" + smsInfoList.get(i).getAddress() + "\n";
            text += "短信内容：" + smsInfoList.get(i).getBody() + "\n\n";
        }
        tvSms.setText(text);
    }

    @Override
    public void onRequestPermissionsResult(
            int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 1) {
            for (int i = 0; i < permissions.length; i++) {
                if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
                    getSms();
                } else {
                    Toast.makeText(
                            this,
                            "权限" + permissions[i] + "申请失败，不能读取系统短信",
                            Toast.LENGTH_SHORT).show();
                }
            }
        }
    }
}